package com.xu.cloud.common.advice.exception;

import com.xu.cloud.common.constant.OrderConstant;
import com.xu.cloud.common.exception.ErrorCodeEnum;
import com.xu.cloud.common.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 数据库异常处理
 *
 * @author xuguan
 * @since 2024/9/4
 */
@Order(OrderConstant.DAO_EXCEPTION_HANDLER_ORDER)
@ConditionalOnClass(value = {DataAccessException.class})
@RestControllerAdvice
@Slf4j
public class DaoExceptionHandlerAdvice {

    @ExceptionHandler(value = DataAccessException.class)
    @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<?> handlerDataAccessException(DataAccessException dataAccessException) {
        log.error("捕获到DataAccessException数据库异常, message: ", dataAccessException);
        return Result.fail(ErrorCodeEnum.INTERNAL_SERVER_ERROR.getCode(), ErrorCodeEnum.INTERNAL_SERVER_ERROR.getMessage(), null);
    }
}
